<template>
  <div class="container">
    <div class="title"></div>
    <button v-if="!loading" :disabled="isDisabled" class="btn" @click="handleClick">摇号</button>
    <button v-else class="btn" @click="handleStop">停止</button>
    <div class="content" v-if="!loading">
      <div class="sun"></div>
      <div class="rainbow"></div>
      <div class="left">
        <div class="left-title">待抽取名单 {{preData.total}}人</div>
        <div class="pre-list">
          <p :key="i.childid" v-for="i in preData.record">{{i.name}}</p>
        </div>
      </div>
      <div class="divider"></div>
      <div class="right" v-if="resultRecord.length > 0">
        <div class="result-list" :key="item.lotteryRound" v-for="item in resultRecord">
          <div class="list-title">第 {{item.lotteryRound}} 轮抽取结果 10人</div>
          <div class="name-list">
            <span :key="i.childid" v-for="i in item.data">{{i.childid}} - {{i.name}}</span>
          </div>
        </div>
      </div>
      <div class="right" v-else>
        <div class="empty-text">
          <p>摇号还未开始</p>
          <p>暂无摇号信息</p>
        </div>
      </div>
    </div>
    <div class="content" v-else>
      <div class="sun"></div>
      <div class="rainbow"></div>
      <LotteryWait :values="preData.record" />
    </div>
    <div class="modal-container" v-if="showModal">
      <div class="modal-box">
        <div class="modal-title">第 {{lotteryData.lotteryRound}} 轮抽取结果x人</div>
        <div class="modal-list">
          <span v-for="i in preData.record">{{i.name}}</span>
        </div>
        <div class="modal-btn" @click="handleOk">确定</div>
      </div>
    </div>
  </div>
</template>

<script>
  import {
    handleLottery,
    getResults,
    preData
  } from "@/api/lottery";
  import LotteryWait from "./LotteryWait";
  import {
    setTimeout
  } from "timers";
  export default {
    components: {
      LotteryWait
    },
    data() {
      return {
        isDisabled: false,
        showModal: false,
        loading: false,
        resultRecord: [],
        resultTotal: "",
        preData: {
          total: 7,
          record: [{
              name: "张三"
            },
            {
              name: "李四"
            },
            {
              name: "王五"
            },
            {
              name: "赵六"
            },
            {
              name: "刘七"
            },
            {
              name: "钱多"
            },
            {
              name: "思聪"
            },
          ]
        },
        lotteryData: {
          lotteryRound: 7,
          record: [{
              name: "张三"
            },
            {
              name: "李四"
            },
            {
              name: "王五"
            },
            {
              name: "赵六"
            },
            {
              name: "刘七"
            },
            {
              name: "钱多"
            },
            {
              name: "思聪"
            },
          ]
        },
      };
    },
    mounted() {},
    methods: {
      onLottery() {
        //跳转抽奖页面
        this.$router.push({
          name: "lotteryWait"
        });
      },
      handleClick() {
        this.isDisabled = true;
        handleLottery()
          .then(res => {
            this.isDisabled = false;
            if (res.code == 204) {
              this.$notification.error({
                message: "提示",
                description: res.resMsg
              });
              return;
            }
            this.loading = true;
            this.lotteryData = res.data;
            this.fetchData();
          })
          .catch(err => {
            this.$notification.error({
              message: "网络异常",
              description: err
            });
            this.isDisabled = false;
          });
      },
      handleStop() {
        this.showModal = true;
        console.log("停止");
      },
      handleOk() {
        this.showModal = false;
        this.loading = false;
      },
      fetchData() {
        // getResults()
        //   .then(res => {
        //     console.log(res);
        //     const datas = res.data.record;
        //     let map = {},
        //       nList = [];
        //     //设置初始key为0
        //     let _nkey = 0;
        //     datas.forEach((item, index) => {
        //       if (index === 0) {
        //         nList.push({
        //           lotteryRound: item.lotteryRound,
        //           data: [item]
        //         });
        //       } else {
        //         let oItem = datas[index - 1];
        //         //和前一个date一致则在当前添加，否则添加至nList
        //         if (item.lotteryRound === oItem.lotteryRound) {
        //           nList[_nkey]["data"].push(item);
        //         } else {
        //           nList.push({
        //             lotteryRound: item.lotteryRound,
        //             data: [item]
        //           });
        //           _nkey++;
        //         }
        //       }
        //     });
        //     this.resultTotal = res.data.total;
        //     this.resultRecord = nList;
        //   })
        //   .catch(err => {
        //     this.$notification.error({
        //       message: "网络异常",
        //       description: err
        //     });
        //   });
        // preData()
        //   .then(res => {
        //     console.log(res);
        //     const data = res.data;
        //     this.preData = res.data;
        //   })
        //   .catch(err => {
        //     this.$notification.error({
        //       message: "网络异常",
        //       description: err
        //     });
        //   });
      },
      cancel() {}
    }
  };
</script>


<style scoped>
  .container {
    height: 100%;
    width: 100%;
    background-image: url(~@/assets/img/bg.png);
    background-size: 100% 100%;
    background-repeat: no-repeat;
  }
  .title {
    position: absolute;
    left: 50%;
    top: 20px;
    transform: translateX(-52%);
    height: 312px;
    width: 848px;
    margin: 0 auto;
    padding-top: 20px;
    background-image: url(~@/assets/img/title.png);
    background-repeat: no-repeat;
  }
  .btn {
    position: fixed;
    right: 10%;
    top: 58px;
    height: 224px;
    width: 250px;
    background-image: url(~@/assets/img/btn.png);
    background-repeat: no-repeat;
    text-align: center;
    color: rgba(255, 223, 66, 1);
    line-height: 224px;
    font-size: 60px;
    font-weight: bold;
    cursor: pointer;
    margin: 0;
    padding: 0;
    border: 1px solid transparent;
    outline: none;
    background-color: transparent;
    z-index: 999;
  }
  .content {
    box-sizing: border-box;
    position: relative;
    width: calc(100% - 120px);
    height: 64%;
    top: 310px;
    left: 60px;
    background: rgba(255, 255, 255, 1);
    border-radius: 16px;
    opacity: 0.9;
    border: 5px solid rgba(255, 213, 0, 1);
  }
  .sun {
    position: absolute;
    top: -169px;
    left: -62px;
    width: 238px;
    height: 224px;
    background-image: url(~@/assets/img/sun.png);
    background-repeat: no-repeat;
  }
  .rainbow {
    position: absolute;
    width: 252px;
    height: 162px;
    top: -115px;
    right: -65px;
    background-image: url(~@/assets/img/rainbow.png);
    background-repeat: no-repeat;
  }
  .left {
    position: absolute;
    left: 49px;
    top: 75px;
    height: calc(100% - 100px);
    width: 297px;
    box-sizing: border-box;
    border-radius: 8px;
    border: 3px solid rgba(254, 207, 11, 1);
  }
  .left-title {
    position: relative;
    top: -24px;
    left: 16px;
    width: 255px;
    height: 48px;
    box-sizing: border-box;
    background: rgba(230, 185, 0, 1);
    border-radius: 24px;
    color: #fff;
    line-height: 48px;
    font-size: 24px;
    font-weight: bold;
    text-align: center;
    z-index: 999;
  }
  .pre-list {
    position: relative;
    top: -20px;
    height: calc(100% - 40px);
    overflow-y: scroll;
    text-align: center;
    margin-right: 8px;
  }
  .pre-list p {
    color: #333330;
    font-size: 25px;
    font-weight: 800;
    margin: 5px 0;
  }
  .divider {
    position: absolute;
    top: 10px;
    left: 23%;
    width: 2px;
    height: calc(100% - 20px);
    background-color: #ff7474;
    opacity: 0.5;
  }
  .right {
    position: absolute;
    right: 20px;
    top: 20px;
    height: calc(100% - 40px);
    width: 1283px;
    box-sizing: border-box;
    overflow-y: scroll;
  }
  .result-list {
    margin-top: 30px;
    margin-bottom: 50px;
    width: 99%;
    box-sizing: border-box;
    border-radius: 8px;
    border: 3px solid rgba(243, 123, 85, 1);
    flex-wrap: wrap;
  }
  .name-list {
    position: relative;
    top: -10px;
  }
  .name-list span {
    display: inline-block;
    flex: 1;
    color: #e66339;
    font-size: 24px;
    font-weight: bold;
    margin-left: 44px;
  }
  .list-title {
    position: relative;
    top: -24px;
    left: 16px;
    width: 255px;
    height: 48px;
    box-sizing: border-box;
    background: rgba(243, 123, 85, 1);
    border-radius: 24px;
    color: #fff;
    line-height: 48px;
    font-size: 24px;
    font-weight: bold;
    text-align: center;
    z-index: 999;
  }
   ::-webkit-scrollbar {
    width: 10px;
    /*对垂直流动条有效*/
    height: 10px;
    /*对水平流动条有效*/
  }
  /*定义滚动条的轨道颜色、内阴影及圆角*/
   ::-webkit-scrollbar-track {
    border-radius: 3px;
  }
  /*定义滑块颜色、内阴影及圆角*/
   ::-webkit-scrollbar-thumb {
    border-radius: 7px;
    background-color: rgba(254, 207, 11, 0.5);
  }
  /*定义右下角汇合处的样式*/
   ::-webkit-scrollbar-corner {
    background: khaki;
  }
  .modal-container {
    height: 100%;
    position: fixed;
    width: 100%;
    top: 0;
    left: 0;
    background-color: rgba(0, 0, 0, 0.5);
  }
  .modal-box {
    width: 1418px;
    height: 873px;
    margin: 0 auto;
    /* background: rgba(255, 255, 255, 1);
        border-radius: 8px; */
    background-image: url(~@/assets/img/modal-bg.png);
    background-repeat: no-repeat;
    position: fixed;
    z-index: 1000;
    left: 50%;
    transform: translateX(-50%);
    bottom: 5%;
  }
  .modal-title {
    position: absolute;
    top: 189px;
    left: 50%;
    transform: translateX(-50%);
    width: 540px;
    height: 96px;
    font-size: 48px;
    line-height: 96px;
    text-align: center;
    color: #fff;
  }
  .modal-list {
    width: 55%;
    margin: 0 auto;
    min-height: 400px;
    color: #e66339;
    font-size: 36px;
    display: flex;
    flex-wrap: wrap;
    box-sizing: border-box;
    padding: 20px;
    margin-top: 322px;
    text-align: center;
  }
  .modal-list span {
    display: inline-block;
    width: 50%;
    margin-top: 10px;
    font-weight: bold;
  }
  .modal-btn {
    margin: 0 auto;
    width: 320px;
    height: 76px;
    font-size: 36px;
    line-height: 76px;
    color: #333333;
    text-align: center;
    cursor: pointer;
    font-weight: bold;
    margin-top: 20px;
  }
  .empty-text {
    font-size: 40px;
    text-align: center;
    position: relative;
    top: 50%;
    transform: translateY(-50%);
  }
  .empty-text p {
    margin: 0;
  }
</style>
